package com.yxy.springcloud.framework.unionauth.controller;

import com.wf.captcha.SpecCaptcha;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.common.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;

/**
 * @author yuxiaoyang
 * @Date 2020-08-04
 * 验证码相关服务
 */
@RestController
@Slf4j
@RequestMapping("/checkCode")
public class CheckCodeController {
  @Autowired
  private RedisUtils redisUtils;

  // 获取 或者 刷新验证码
  @GetMapping("/get")
  public RestResponse getCheckCode() {
    SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
    String verCode = specCaptcha.text().toLowerCase();
    String key = "checkCode#"+UUID.randomUUID().toString();
    // 存入redis并设置过期时间为30分钟
    redisUtils.setEx(key, verCode, 60*10);//有效期10min
    // 将key和base64返回给前端
    RestResponse restResponse = new RestResponse(200, "成功");
    HashMap<String, String> resultMap = new HashMap<>();
    resultMap.put("key", key);
    resultMap.put("image", specCaptcha.toBase64());
    restResponse.setContent(resultMap);
    return restResponse;
  }
}
